package com.dylan.datacollection;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.bumptech.glide.Glide;
import com.bumptech.glide.manager.ConnectivityMonitor;
import com.dylan.datacollection.db.SharedPreferencesUtils;
import com.dylan.datacollection.entity.UserEntity;
import com.dylan.datacollection.net.HttpConfig;
import com.dylan.datacollection.net.HttpHelper;
import com.dylan.datacollection.net.ResponseCallback;
import com.dylan.datacollection.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class ChangePwdActivity extends AppCompatActivity implements View.OnClickListener{
    EditText pwd1;
    EditText pwd2;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        context = this;
        pwd1 = (EditText) findViewById(R.id.pwd_editText1);
        pwd2 = (EditText) findViewById(R.id.pwd_editText2);

        findViewById(R.id.submit_pwd_button).setOnClickListener(this);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        String pwd1String = pwd1.getText().toString();
        String pwd2String = pwd2.getText().toString();
        if (!TextUtils.isEmpty(pwd1String)&&!TextUtils.isEmpty(pwd2String)){
            Gson gson = new Gson();
            UserEntity userEntity = gson.fromJson(SharedPreferencesUtils.readString(context,"user",""),UserEntity.class);
            changePwd(pwd1String,pwd2String,userEntity.getId());
        }else{
            ToastUtils.showLongMsg(this,"密码不能为空！");
        }

    }
    public void changePwd(String pwd,String newPwd,String uid){
        HttpHelper.getInstance().changePwd(pwd,newPwd,uid,new ResponseCallback(){
            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                try {
                    Log.i("pwd","response :"+response.toString());
                    JSONObject jsonObject = new JSONObject(response.toString());
                    if (jsonObject.optInt("status") == HttpConfig.SUCCESS){
                        ToastUtils.showShortMsg(ChangePwdActivity.this,"您的密码已修改成功，下次登陆请使用新密码。");
                        SharedPreferencesUtils.saveToken(ChangePwdActivity.this,"");
                        finish();
                    }else{
                        ToastUtils.showShortMsg(ChangePwdActivity.this,jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }
        });
    }
}
